// 主进程文件
const { app, BrowserWindow, ipcMain } = require('electron')
const path = require('node:path')

const createWindow = () => {
    const win = new BrowserWindow({
      width: 800,
      height: 600,
      frame: false,
      webPreferences: {
        preload: path.join(__dirname, '/src/js/preload.js')
      }
    })

    // ipcMain.on('set-title', (event, title) => {
    //   const webContents = event.sender
    //   const win = BrowserWindow.fromWebContents(webContents)
    //   win.setTitle(title)
    // })

    win.loadFile('./src/html/index.html')
}

// 在应用准备就绪时调用函数
app.on('ready', () => {
    // ipcMain.handle('ping', () => 'pong')
    createWindow()

    app.on('activate', () => {
        if (BrowserWindow.getAllWindows().length === 0) {
          createWindow()
        }
    })
})

// 关闭所有窗口时退出应用
app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') app.quit()
})